#coding=utf-8
'''
@这是一个视频客流设备烧写硬件信息的软件，软件通过自动发现全志H3的SD卡系统盘，将按照填写的配置自动生成配置文件保存在SD卡
@中，没张SD卡系统盘之可以烧写一次。
@
@
@
'''
from Tkinter import *
import _winreg
import os
import pickle
import shutil


def GetPartitionNames():
    res = []
    deviceID = ''
    subKey = 'SYSTEM\MountedDevices'

    key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, subKey)
    i = 0
    try:
        while True:
            name,value,type = _winreg.EnumValue(key,i)
            if name.startswith('\\DosDevices\\'):
                res.append((name, repr(value)[1:17]))
                if name.startswith('\\DosDevices\\C'):
                    deviceID = repr(value)[1:17]
            i+=1
    except WindowsError:
        pass
    res = filter(lambda item : item[1] == deviceID, res)
    res = zip(*res)[0]
    index = res[0].rindex('\\')
    res = sorted([item[index+1:-1] for item in res])
    return res

def keyPress(a):
    l = list(macEntry.get())
    for i in range(len(l) - 1, -1, -1):
        if not(48 <= ord(l[i]) <= 57 or ord(l[i]) == 46):
            macEntry.delete(i, i+1)

def burnBottonProcess():
    disk = 'E'
    resultTexttk.delete(0.0,END)
    data = {'Mac':'','Public':''}
    data['Mac'] = macText.get()
    data['Public'] = urlText.get()
    f1 = open("deviceConfig.flw", 'wb')
    pickle.dump(data, f1, True)
    f1.close()
    if os.path.isfile('E:/script.bin') == True:
        disk = 'E'
    elif os.path.exists('F:/script.bin') == True:
        disk = 'F'
    elif os.path.exists('G:/script.bin') == True:
        disk = 'G'
    else:
        showText = "不能自动发现磁盘，请选择"
        resultText.set(showText)
        resultTexttk.insert(END,resultText.get())
        return False

    if os.path.isfile(str(disk)+':/deviceConfig.flw') == True:
        f1 = open(str(disk)+':/deviceConfig.flw','rb')
        data = pickle.load(f1)
        f1.close()
        showText = "这个设备已经烧录配置信息\n\rMAC:"+str(data['Mac'])+'\r\n'+'Url:'+str(data['Public'])
        resultText.set(showText)
        resultTexttk.insert(END,resultText.get())
    else:
        #拷贝文件
        shutil.copy('deviceConfig.flw',str(disk)+':/deviceConfig.flw')
        tmp = int(macText.get())
        tmp += 1
        macText.set(str(tmp))

        showText = "烧写成功\r\n"+'MAC:'+str(macText.get())+'Url:'+'\r\n'+str(urlText.get())+'\r\n'
        resultText.set(showText)
        resultTexttk.insert(END,resultText.get())

def clearBottonProcess():
    macText.set('')
    resultTexttk.delete(0.0,END)

root = Tk()
macVar = StringVar()
urlVar = StringVar()
macText = StringVar()
urlText = StringVar()
resultVar = StringVar()
resultText = StringVar()

macLabel = Label( root, text = "MAC地址")
urlLabel = Label( root, text = "url地址")
macLabel.grid(row=0,column=0)
macEntry = Entry(root,bd=5,textvariable=macText)
macEntry.bind('<KeyRelease>', keyPress)
macEntry.grid(row=0,column=1)

urlLabel.grid(row=1,column=0)
urlEntry = Entry(root,bd=5,textvariable=urlText)
urlText.set("lijun.b")
urlEntry.grid(row=1,column=1)

resultLabel = Label(root,text='结果:')
resultLabel.grid(row=2,column=0)

resultTexttk = Text(root)
resultTexttk.grid(row=2,column=1)

Button(root, text='烧写', width=19,relief=GROOVE,bg="red",command=burnBottonProcess).grid(row=3,column=3)
Button(root, text='清除', width=10,relief=GROOVE,bg="green",command=clearBottonProcess).grid(row=0,column=3)

root.mainloop()